package slhs.tempo.TableAnalyzer;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class SearchTextChangeListener implements DocumentListener {

    private TablePanelHandler tablePanelHandlerObject;
    private FilterControlsPanelHandler filterControlsPanelHandlerObject;
    private static final String COMMA = ",";
    private static final String OR = "|";

    public SearchTextChangeListener(TablePanelHandler tablePanelHandlerObject,
	    FilterControlsPanelHandler filterControlsPanelHandlerObject) {
	this.tablePanelHandlerObject = tablePanelHandlerObject;
	this.filterControlsPanelHandlerObject = filterControlsPanelHandlerObject;

    }

    private String getSearchRegularExpression() {
	String filterText = filterControlsPanelHandlerObject.getTextFilterText();
	return filterText.replace(COMMA, OR);

    }

    @Override
    public void changedUpdate(DocumentEvent event) {
	if (filterControlsPanelHandlerObject.hasTextSearchActive())
	    tablePanelHandlerObject.plainTextFilter(getSearchRegularExpression());
    }

    @Override
    public void insertUpdate(DocumentEvent event) {
	if (filterControlsPanelHandlerObject.hasTextSearchActive())
	    tablePanelHandlerObject.plainTextFilter(getSearchRegularExpression());
    }

    @Override
    public void removeUpdate(DocumentEvent event) {
	if (filterControlsPanelHandlerObject.hasTextSearchActive())
	    tablePanelHandlerObject.plainTextFilter(getSearchRegularExpression());
    }

}
